package com.atguigu.gulimall.seckill.interceptor;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class SeckillWebIntereptor implements HandlerInterceptor {
    public static  ThreadLocal<MemberVo> loginUser = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断用户是否登录
        MemberVo member = (MemberVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
        if(member!=null){
            //已登录,放行
            loginUser.set(member);
            return true;
        }
        //未登录，重定向到登录页
        request.getSession().setAttribute("msg","您还未登录，请登录");
        response.sendRedirect("http://auth.gulimall.com/login.html");
        return false;
    }

}
